Skip to content

3. modularize session context - #2190

Open
kevinjosethomas wants to merge 8 commits into
refactor/session-input-schedulerfrom
refactor/session-context
Open

3. modularize session context#2190
kevinjosethomas wants to merge 8 commits into
refactor/session-input-schedulerfrom
refactor/session-context

Conversation

@kevinjosethomas

@kevinjosethomas kevinjosethomas commented Sep 10, 2026

Copy link
Copy Markdown
Member

This is part of the stack giving Prime Agent session features explicit owners. This PR moves compaction, refinement, and continuation state and cleanup out of AgentSession into focused session modules, while preserving persistence, cancellation, event ordering, public dispatch, and custom-reviewer behavior.

It also fixes two existing CodeQL findings surfaced during review: Codex model URL normalization now takes linear time, and Python skill import names are passed as data instead of inserted into executable Python source. The unused SerializedBackgroundPlanResult facade re-export is removed; its owning module retains the type.

Rebased on main at 5255a6b. Repository checks pass at this PR head, and 31 focused bootstrap and model-discovery regressions pass. Earlier compaction/refinement coverage is recorded in the cleanup issue; GitHub checks and benchmarks must finish on the updated commits. Macroscope's full correctness review is still blocked by its per-review cost limit.

Stacked on #2185; tracks ENG-5938.

Note

Modularize AgentSession into compaction, continuation, and refinement coordinators

  • Extracts SessionCompaction, SessionContinuation, SessionRefinement, AutoRefinement, and RefinementExecution from AgentSession in agent-session.ts, with each coordinator owning its state, abort controllers, and lifecycle via host callback interfaces.
  • Compaction execution moves to compaction-execution.ts, adding extension-provided results, cancellation, and semantic request tracking for summary calls.
  • Continuation scheduling moves to continuation.ts, waiting on agent, retry, refinement, queued-work, and compaction barriers before continuing.
  • Automatic refinement moves to auto-refinement.ts, handling turn-interval and compact triggers, review cooldowns, and branch invalidation.
  • Hardens Python skill imports in bootstrap.ts to pass module names as importlib arguments instead of interpolating into Python source, and fixes Codex model URL trailing-slash normalization in model-registry.ts.
  • Adds harness-state digest computation and persistence to AgentSession, inserting a custom digest message when the newest stored or retained digest differs.
  • Risk: all test-only internal session shapes (SerializedInternals, SessionInternals, etc.) are restructured to nested refinement/compaction/continuation components; out-of-tree consumers relying on flat _compaction or _refinement fields on AgentSession will break. Compaction and refinement status now read from coordinator state instead of removed in-class fields.

Macroscope summarized 5127774.


Note

Medium Risk
Large refactor of compaction, continuation, and refinement orchestration in the core agent loop; behavior is intended to be preserved but regressions would affect session persistence, cancellation, and headless idle semantics.

Overview
Modularizes session context by moving compaction, post-compaction continuation, and harness refinement out of AgentSession into dedicated session/* coordinators wired through typed host callbacks. Public entry points (compact, refine, host compact.* / refine.*, events, abort/disposal) delegate to the new owners while preserving the documented lifecycle rules (disconnect/reconnect ordering, continuation settlement, serialized refine checkpoints, overflow/threshold behavior).

Compaction splits into SessionCompaction (pending requests, automatic/manual runs, overflow recovery, outcome messages) and performSessionCompaction in compaction-execution.ts (extensions, semantic-edge accounting, transcript rebuild). Continuation moves to SessionContinuation with one-shot settlement and commit-fence gating before agent.continue(). Refinement consolidates in SessionRefinement with AutoRefinement and execution helpers for planning/apply, serialized background plans, disposal drains, and branch invalidation. README adds a session-context ownership table and test pointers.

Hardening: Codex model URL normalization drops regex trailing-slash stripping for a linear scan, and kernel bootstrap checks Python skill imports via importlib.import_module(sys.argv[1]) instead of embedding names in -c source.

Reviewed by Cursor Bugbot for commit 5127774. Bugbot is set up for automated code reviews on this repo. Configure here.

@kevinjosethomas kevinjosethomas added the no-changelog Internal change with no user-visible changelog entry label Sep 10, 2026
@macroscopeapp

macroscopeapp Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Macroscope skipped reviewing this pull request. Per-review cost limit exceeded (workspace setting).

This review would cost an estimated $11.11, which exceeds your per-review limit of $10.00.

The top 3 files driving up this estimate:

File Diff Size Estimate
packages/coding-agent/src/core/agent-session.ts 123.36KB $6.17
packages/coding-agent/src/session/refinement.ts 34.30KB $1.72
packages/coding-agent/src/session/compaction.ts 19.56KB $0.98

Tip

To get this pull request reviewed, you can:

  1. Comment @macroscope-app on this PR to request a manual review (monthly spend limits still apply).
  2. Exclude the file(s) above from review by adding a pattern to your .macroscope/ignore.md — note that creating this file replaces Macroscope's built-in default ignores rather than extending them.
  3. Raise your cost limit in your workspace billing settings.

Turn off this reminder going forward

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread packages/coding-agent/src/session/compaction.ts Outdated

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread packages/coding-agent/src/session/auto-refinement.ts
@github-actions

github-actions Bot commented Sep 10, 2026

Copy link
Copy Markdown

Prime Agent performance — completed

PR 51277749 compared with main 5255a6b8.

Overall: 0 regressed · 0 improved · 17 no clear change.

Metric Main This PR Change
Cold startup 3,083.5 ms 2,886.1 ms ≈ -197.4 ms (-6.40%)
Warm startup 1,793.8 ms 1,769.3 ms ≈ -24.6 ms (-1.37%)
Installation 28.26 s 27.36 s ≈ -0.91 s (-3.20%)
Compressed release artifacts 11.13 MB 11.16 MB ≈ +0.03 MB (+0.27%)
Installed footprint 597.55 MB 597.73 MB ≈ +0.18 MB (+0.03%)
Idle memory, summed RSS 1,034.91 MB 1,037.36 MB ≈ +2.45 MB (+0.24%)

Python runtime

Metric Main This PR Change
Python kernel startup 121.0 ms 122.2 ms ≈ +1.3 ms (+1.06%)
Python cell round trip 0.517 ms 0.532 ms ≈ +0.015 ms (+2.81%)
Empty bash command 10.8 ms 10.8 ms ≈ +0.0013 ms (+0.01%)
Bash git status 15.9 ms 16.4 ms ≈ +0.5 ms (+3.09%)
Bash 32 KiB output 11.7 ms 11.3 ms ≈ -0.4 ms (-3.03%)
35 cells / 9 shell calls 159.4 ms 157.7 ms ≈ -1.7 ms (-1.06%)
Python interrupt to done 1.600 ms 1.521 ms ≈ -0.079 ms (-4.96%)
Python state snapshot 24.5 ms 25.0 ms ≈ +0.5 ms (+1.94%)
Python state restore 331.7 ms 353.8 ms ≈ +22.0 ms (+6.65%)
Python idle RSS 34.38 MB 36.21 MB ≈ +1.83 MB (+5.32%)
Python RSS after pandas workload 97.95 MB 98.33 MB ≈ +0.38 MB (+0.39%)

Sandbox cost: ~$0.0886 — no inference calls.
Run, logs, and downloadable raw results

Methodology and samples

Main resolved at 2026-09-11T19:29:33.532386+00:00. Harness 5255a6b8.
Linux x64, 4 vCPU, 8 GB RAM, 20 GB disk; region us.
Image: node:24-bookworm@sha256:be23f54a88d34e8824c741b19b91064094f92c1c97b194144bfc8b50d67258e2.
Stock tools, skills, daemon, and Python bootstrap enabled; fresh homes and a fixed Git fixture.
Onboarding is dismissed; the editor starts without a selected model or submitted prompt.
Medians shown. Arrows require a 20% timing/memory change plus absolute floors and IQR.
These practical noise floors are not a statistical significance test.
Cold means stopped Prime processes; OS filesystem caches are not flushed.
No model requests or credentials. Installation excludes build/setup time.
Installer tarballs use loopback; npm/Python downloads use the network with fresh caches.
Artifact size counts release tarballs; footprint after first use includes registry packages.
MB is decimal. Summed RSS can double-count shared pages; PSS is recorded when available.
Provisioning, setup, and build durations are recorded separately in the raw results.
Kernel probes use the installed JSONL runtime, outside the TUI/TypeScript host.
Per trial: 50 Python cells, 5 calls per shell case, and one 35-cell mix (9 git status calls).
Cell/shell values are batch means; other runtime timings are single operations.
State fixture: a 10,000-row × 8-column integer DataFrame and a 10,000-integer list.
Restore runs in a fresh kernel, including pandas imports; kernel startup is excluded.
Kernel RSS covers the isolated Python process; loaded RSS follows the pandas workload.
Costs estimate full sandbox lifetimes at configured rates, including setup and build.
Budget target: $1; not a billing cap. Checks are informational.

Metric Main successful/attempted PR successful/attempted Main spread PR spread
Cold startup 10/10 10/10 IQR 172.6 ms IQR 85.3 ms
Warm startup 10/10 10/10 IQR 95.5 ms IQR 106.9 ms
Installation 3/3 3/3 range 0.77 s range 1.87 s
Compressed release artifacts 1/1 1/1
Installed footprint 1/1 1/1
Idle memory, summed RSS 10/10 10/10 IQR 103.16 MB IQR 67.82 MB
Python kernel startup 10/10 10/10 IQR 3.7 ms IQR 2.6 ms
Python cell round trip 10/10 10/10 IQR 0.063 ms IQR 0.084 ms
Empty bash command 10/10 10/10 IQR 0.7 ms IQR 0.6 ms
Bash git status 10/10 10/10 IQR 1.0 ms IQR 1.3 ms
Bash 32 KiB output 10/10 10/10 IQR 1.8 ms IQR 1.2 ms
35 cells / 9 shell calls 10/10 10/10 IQR 15.4 ms IQR 10.1 ms
Python interrupt to done 10/10 10/10 IQR 0.338 ms IQR 0.124 ms
Python state snapshot 10/10 10/10 IQR 0.8 ms IQR 1.7 ms
Python state restore 10/10 10/10 IQR 16.5 ms IQR 20.7 ms
Python idle RSS 10/10 10/10 IQR 3.91 MB IQR 3.34 MB
Python RSS after pandas workload 10/10 10/10 IQR 4.22 MB IQR 3.57 MB

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 827d1d4. Configure here.

Comment thread packages/coding-agent/src/core/agent-session.ts Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog Internal change with no user-visible changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant